﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;

using Develore.GDiff;
using System.Drawing.Imaging;

namespace GDiffLibTests
{
	[TestClass]
	public class GDiffTests
	{

		#region Test: public void ApplyGDiff01()

		[global::Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute()]
		[global::Microsoft.VisualStudio.TestTools.UnitTesting.DescriptionAttribute(" ")]
		public void ApplyGDiff01()
		{
			string s = GDiff.Apply(Properties.Resources.GDIFF_Google1a_Google1b, Properties.Resources.Google01a);
			Assert.AreEqual<string>(Properties.Resources.Google01b, s);
		}

		#endregion

		#region Test: public void ApplyGDiff02()

		[global::Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute()]
		[global::Microsoft.VisualStudio.TestTools.UnitTesting.DescriptionAttribute(" ")]
		public void ApplyGDiff02()
		{
			byte[] target = null, source = null;

			using (MemoryStream strm = new MemoryStream())
			{
				Properties.Resources.Image02.Save(strm, ImageFormat.Jpeg);
				strm.Position = 0;

				source = new byte[strm.Length];
				strm.Read(source, 0, source.Length);
			}
			using (MemoryStream strm = new MemoryStream())
			{
				Properties.Resources.Image03.Save(strm, ImageFormat.Jpeg);
				strm.Position = 0;

				target = new byte[strm.Length];
				strm.Read(target, 0, target.Length);
			}

			byte[] result = GDiff.Apply(Properties.Resources.GDIFF_Image02_Image03, source);
			CollectionAssert.AreEqual(target, result);
		}

		#endregion
		


		#region Test: public void ComputeGDiff01()

		[global::Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute()]
		[global::Microsoft.VisualStudio.TestTools.UnitTesting.DescriptionAttribute(" ")]
		public void ComputeGDiff01()
		{
			byte[] sourceBuffer = new byte[2048];
			byte[] targetBuffer = new byte[2048];

			Random rnd = new Random();
			rnd.NextBytes(sourceBuffer);

			Array.Copy(sourceBuffer, targetBuffer, sourceBuffer.Length);
			targetBuffer[1234] = (byte)((targetBuffer[1234] + 1) ^ 256);

			using (MemoryStream gdiff = new MemoryStream())
			{
				using (MemoryStream source = new MemoryStream())
				{
					source.Write(sourceBuffer, 0, sourceBuffer.Length);
					source.Position = 0;
					using (MemoryStream target = new MemoryStream())
					{
						target.Write(targetBuffer, 0, targetBuffer.Length);
						target.Position = 0;

						GDiff.Compute(source, target, gdiff);
					}
				}

				Assert.IsFalse(0 == gdiff.Position);
				Assert.IsTrue(gdiff.Length > 5);
			}
		}

		#endregion

		#region Test: public void ComputeGDiff02()

		[global::Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute()]
		[global::Microsoft.VisualStudio.TestTools.UnitTesting.DescriptionAttribute(" ")]
		public void ComputeGDiff02()
		{
			MemoryStream source = new MemoryStream(new byte[] { 1, 2, 3, 4 });
			MemoryStream target = new MemoryStream(new byte[] { 1, 2, 3, 4 });

			try
			{
				using (MemoryStream result = new MemoryStream())
				{
					GDiff.Compute(source, target, result, 2);
					Assert.IsFalse(result.Position == 0);
				}

			}
			finally
			{
				source.Dispose();
				target.Dispose();
			}
		}

		#endregion

		#region Test: public void ComputeGDiff03()

		[global::Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute()]
		[global::Microsoft.VisualStudio.TestTools.UnitTesting.DescriptionAttribute(" ")]
		public void ComputeGDiff03()
		{
			MemoryStream source = new MemoryStream(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 });
			MemoryStream target = new MemoryStream(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 17, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 });

			try
			{
				using (MemoryStream result = new MemoryStream())
				{
					GDiff.Compute(source, target, result, 8);
					Assert.IsFalse(result.Position == 0);
				}
			}
			finally
			{
				source.Dispose();
				target.Dispose();
			}
		}

		#endregion

		#region Test: public void ComputeGDiff04()

		[global::Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute()]
		[global::Microsoft.VisualStudio.TestTools.UnitTesting.DescriptionAttribute(" ")]
		public void ComputeGDiff04()
		{
			Assert.AreNotEqual<string>(Properties.Resources.Google01a, Properties.Resources.Google01b);
			byte[] gdiff = GDiff.Compute(Properties.Resources.Google01a, Properties.Resources.Google01b);
			string s = GDiff.Apply(gdiff, Properties.Resources.Google01a);
			Assert.AreEqual<string>(Properties.Resources.Google01b, s);
		}

		#endregion

		#region Test: public void ComputeGDiff05()

		[global::Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute()]
		[global::Microsoft.VisualStudio.TestTools.UnitTesting.DescriptionAttribute(" ")]
		public void ComputeGDiff05()
		{
			byte[] gdiff = GDiff.Compute(Properties.Resources.Google01b, Properties.Resources.Google01b);
			string s = GDiff.Apply(gdiff, Properties.Resources.Google01b);
			Assert.AreEqual<string>(Properties.Resources.Google01b, s);
		}

		#endregion

		#region Test: public void ComputeGDiff06()

		[global::Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute()]
		[global::Microsoft.VisualStudio.TestTools.UnitTesting.DescriptionAttribute(" ")]
		public void ComputeGDiff06()
		{
			byte[] gdiff = GDiff.Compute(Encoding.UTF8.GetBytes(Properties.Resources.Google01a), Encoding.UTF8.GetBytes(Properties.Resources.Google01b), 1024);
			string s = GDiff.Apply(gdiff, Properties.Resources.Google01a);
			Assert.AreEqual<string>(Properties.Resources.Google01b, s);
		}

		#endregion

		#region Test: public void ComputeGDiff07()

		[global::Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute()]
		[global::Microsoft.VisualStudio.TestTools.UnitTesting.DescriptionAttribute(" ")]
		public void ComputeGDiff07()
		{
			byte[] gdiff = GDiff.Compute(Properties.Resources.Word01a, Properties.Resources.Word01b, 4096);
			byte[] document = GDiff.Apply(gdiff, Properties.Resources.Word01a);
			CollectionAssert.AreEqual(Properties.Resources.Word01b, document);
		}

		#endregion

		#region Test: public void ComputeGDiff08()

		[global::Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute()]
		[global::Microsoft.VisualStudio.TestTools.UnitTesting.DescriptionAttribute(" ")]
		public void ComputeGDiff08()
		{
			byte[] gdiff = GDiff.Compute(Properties.Resources.Word01b, Properties.Resources.Word01c);
			byte[] document = GDiff.Apply(gdiff, Properties.Resources.Word01b);
			CollectionAssert.AreEqual(Properties.Resources.Word01c, document);
		}

		#endregion

		#region Test: public void ComputeGDiff09()

		[global::Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute()]
		[global::Microsoft.VisualStudio.TestTools.UnitTesting.DescriptionAttribute(" ")]
		public void ComputeGDiff09()
		{
			byte[] gdiff = GDiff.Compute(Properties.Resources.Word01a, Properties.Resources.Word01a);
			byte[] document = GDiff.Apply(gdiff, Properties.Resources.Word01a);
			CollectionAssert.AreEqual(Properties.Resources.Word01a, document);
		}

		#endregion

		#region Test: public void ComputeGDiff10()

		[global::Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute()]
		[global::Microsoft.VisualStudio.TestTools.UnitTesting.DescriptionAttribute(" ")]
		public void ComputeGDiff10()
		{
			byte[] gdiff = GDiff.Compute(Properties.Resources.Google2008Q1, Properties.Resources.Google2008Q2);
			byte[] document = GDiff.Apply(gdiff, Properties.Resources.Google2008Q1);
			CollectionAssert.AreEqual(Properties.Resources.Google2008Q2, document);
		}

		#endregion

		#region Test: public void ComputeGDiff11()

		[global::Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute()]
		[global::Microsoft.VisualStudio.TestTools.UnitTesting.DescriptionAttribute(" ")]
		public void ComputeGDiff11()
		{
			byte[] source, target;

			using (MemoryStream strm = new MemoryStream())
			{
				Properties.Resources.Image01a.Save(strm, ImageFormat.Jpeg);
				strm.Position = 0;

				source = new byte[strm.Length];
				strm.Read(source, 0, source.Length);
			}
			using (MemoryStream strm = new MemoryStream())
			{
				Properties.Resources.Image01b.Save(strm, ImageFormat.Jpeg);
				strm.Position = 0;

				target = new byte[strm.Length];
				strm.Read(target, 0, target.Length);
			}

			byte[] gdiff = GDiff.Compute(source, target);
			byte[] image = GDiff.Apply(gdiff, source);

			CollectionAssert.AreEqual(target, image);
		}

		#endregion

		#region Test: public void ComputeGDiff12()

		[global::Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute()]
		[global::Microsoft.VisualStudio.TestTools.UnitTesting.DescriptionAttribute(" ")]
		public void ComputeGDiff12()
		{
			byte[] source, target;

			using (MemoryStream strm = new MemoryStream())
			{
				Properties.Resources.Image02.Save(strm, ImageFormat.Jpeg);
				strm.Position = 0;

				source = new byte[strm.Length];
				strm.Read(source, 0, source.Length);
			}
			using (MemoryStream strm = new MemoryStream())
			{
				Properties.Resources.Image03.Save(strm, ImageFormat.Jpeg);
				strm.Position = 0;

				target = new byte[strm.Length];
				strm.Read(target, 0, target.Length);
			}

			byte[] gdiff = GDiff.Compute(source, target);
			byte[] image = GDiff.Apply(gdiff, source);

			CollectionAssert.AreEqual(target, image);
		}

		#endregion

		#region Test: public void ComputeGDiff13()

		[global::Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute()]
		[global::Microsoft.VisualStudio.TestTools.UnitTesting.DescriptionAttribute(" ")]
		public void ComputeGDiff13()
		{
			byte[] diff = GDiff.Compute("desc", "c");
			string s = GDiff.Apply(diff, "desc");

			Assert.AreEqual<string>("c", s);
		}

		#endregion
		
	}

}
